<?php 
require('Survey.php');
require('SurveySection.php');
require('SurveyQuestion.php');
require('SurveyDemography.php');
require('../includes/core/katana/cores/survey/SurveyResponse.php');
class SurveyAdmin
{
	public $SiteId;
	public $Surveys;
	public $Item;
	public $Survey;
	public $SubLoaded;
	public $AjaxRun;
	public $Response;
	private $Commander;
	
	public function SurveyAdmin() 
	{ 
		global $commander;
		$this->SubLoaded = false;
		$this->Commander = $commander;
		self::setSiteId(SITE_ID);
		if(defined('AJAX_RUN'))
		{ self::setAjaxRun(true); }
		else
		{ self::setAjaxRun(false); }
		self::runPreActions();
		self::loadSurveys();
		if($_GET['s'] > 0)
		{ self::loadSurvey($_GET['s']); }
		self::runPostActions();
		self::loadSurveys();
	}
	
	public function getSiteId() { return $this->SiteId; }
	public function getItem() { return $this->Item; }
	public function getSurvey() { return $this->Survey; }
	public function getSurveys() { return $this->Surveys; }
	public function getSubLoaded() { return $this->SubLoaded; }
	public function getAjaxRun() { return $this->AjaxRun; }
	public function getResponse() { return $this->Response; }
	
	public function setSiteId($val) { $this->SiteId=$val; }
	public function setItem($val) { $this->Item=$val; }
	public function setSurvey($val) { $this->Survey=$val; }
	public function setSurveys($val) { $this->Surveys=$val; }
	public function setSubLoaded($val) { $this->SubLoaded=$val; }
	public function setAjaxRun($val) { $this->AjaxRun=$val; }
	public function setResponse($val) { $this->Response=$val; }
	
	public function loadSurveys()
	{
		if(($this->SiteId > 0) && ($this->SubLoaded == false))
		{
			$sql = 'SELECT id, site_id, title, password, active FROM '.DB_PERFIX.'survey WHERE site_id='.$this->SiteId.' ORDER BY id ASC;';
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					$this->Surveys[$row['id']] = new Survey();
					$this->Surveys[$row['id']]->setId($row['id']);
					$this->Surveys[$row['id']]->setSiteId($row['site_id']);
					$this->Surveys[$row['id']]->setTitle($row['title']);
					$this->Surveys[$row['id']]->setPassword($row['password']);
					$this->Surveys[$row['id']]->setActive($row['active']);
					$this->Surveys[$row['id']]->loadSections();
				}
			}
			$this->SubLoaded = true;
		}
	}
	
	public function loadSurvey($id)
	{
		if($this->SiteId > 0)
		{
			if(isset($this->Surveys[$id]))
			{ 
				$this->Survey = new Survey();
				$this->Survey = $this->Surveys[$id];

				$sql = 'SELECT description, guide FROM '.DB_PERFIX.'survey WHERE id='.$this->Survey->getId().';';
				$result = mysql_query($sql);
				if($result)
				{
					while($row = mysql_fetch_array($result))
					{
						$this->Survey->setDescription($row['description']);
						$this->Survey->setGuide($row['guide']);
					}
				}
				if($_REQUEST['sec'] > 0)
				{ $this->Survey->loadSection($_REQUEST['sec']); }
				
				if(($_REQUEST['i'] > 0) || ($_REQUEST['stat'] > 0))
				{
					if($this->Survey->Sections)
					{
						foreach($this->Survey->Sections as $sec)
						{ 
							if($_REQUEST['stat'] > 0)
							{ $this->Survey->loadSection($sec->Id); }
							$sec->loadQuestions(); 
						}
						$this->Survey->loadResponses();
						if($_REQUEST['stat'] > 0)
						{ 
							if($this->Survey->Responses)
							{
								foreach($this->Survey->Responses as $res)
								{ $res->LoadValues(); }
							}
						}
						if($_GET['r'] > 0)
						{ $this->Survey->loadResponse($_GET['r']); }
					}
					$this->Survey->loadDemogs();
				}
				else
				{ $this->Survey->loadDemogs(); }
				$this->Item = new Survey();
				$this->Item = $this->Survey;
			}
		}
	}

	function getSurveysMenuHTML()
	{
		self::loadSurveys();
		global $commander;
		if($this->Surveys)
		{
			$output .= '<ul>';
			foreach($this->Surveys as $survey)
			{
				$style[0] = ' style="background-image:url('.SYSICONS_URL.'treemenu/survey_16.gif)" ';
				$style[1] = ' style="background-image:url('.SYSICONS_URL.'treemenu/file_doc_16.gif)" ';
				
				$class = '';
				if($_REQUEST['s'] == $survey->getId())
				{ $class = 'open'; }
				
				$output .= 	'<li id="s_'.$survey->getId().'" class="'.$class.' " '.$state.' rel="survey">';
				$output .= 		'<a '.$style[0].' href="?con='.$commander->Con.'&amp;s='.$survey->getId().'">';
				$output .= 			$survey->getTitle();
				if($survey->Sections)
				{ $output .= 		'&nbsp;('.count($survey->Sections).')'; }
				$output .= 		'</a>';
				
				if($survey->Sections)
				{
					$output .= '<ul>';
					foreach($survey->Sections as $section)
					{
						$class = '';
						if($_REQUEST['sec'] == $survey->getId())
						{ $class = 'selected'; }
						
						$output .= '<li id="sec_'.$section->getId().'" class="'.$class.' leaf" '.$state.' rel="section">';
						$output .= 		'<a '.$style[1].' href="?con='.$commander->Con.'&amp;s='.$survey->getId().'&amp;sec='.$section->getId().'">';
						$output .= 			$section->getTitle();
						$output .= 		'</a>';
						$output .= '</li>';
					}
					$output .= 	'</ul>';
				}
				$output .= 	'</li>';	
			}
			$output .= '</ul>';
		}
		return $output;
	}
	
	public function runPreActions()
	{
		switch ($_POST['action'])
		{
			case 'list_s':
				if($_POST['new_s'][0])
				{ self::addSurvey($_POST['new_s'][0]); }
				return;
			break;
			case 'editor_s':
				if($_POST['new_sec'])
				{ self::addSurveySection($_REQUEST['s'], $_POST['new_sec_type'], $_POST['new_sec']); }
				if($_POST['new_dem'])
				{ self::addSurveyDemog($_REQUEST['s'], $_POST['new_dem']); }
				return;
			break;
			case 'editor_sec':
				if($_POST['new_ques'])
				{ self::addSurveyQuestion($_REQUEST['s'], $_REQUEST['sec'], $_POST['new_ques_type'], $_POST['new_ques']); }
				return;
			break;
		}
	}
	
	public function runPostActions()
	{
		global $commander;
		switch ($_POST['action'])
		{
			case 'list_s':
				$i=0;
				foreach($_POST['s_id'] as $id)
				{
					if($_POST['delete_stat'] == $this->Surveys[$id]->Id)
					{ $this->Surveys[$id]->clearResponses();  }
				
					if(stripslashes($_POST['s_title'][$i]) != $this->Surveys[$id]->getTitle())
					{ 
						$this->Surveys[$id]->setTitle(stripslashes($_POST['s_title'][$i]));
						$this->Surveys[$id]->setDataChanged(true);
					}
					if(stripslashes($_POST['s_password'][$i]) != $this->Surveys[$id]->getPassword())
					{ 
						$this->Surveys[$id]->setPassword(stripslashes($_POST['s_password'][$i]));
						$this->Surveys[$id]->setDataChanged(true);
					}
					$this->Surveys[$id]->update();
					$i++;
				}
				if($_POST['activate'] > 0)
				{ 
					$this->Surveys[$_POST['activate']]->setActive(1);
					$commander->setTableActive($_POST['activate'], 'survey', 1); 
				}
				if($_POST['deactivate'] > 0)
				{ 
					$this->Surveys[$_POST['deactivate']]->setActive(0);
					$commander->setTableActive($_POST['deactivate'], 'survey', 0);  
				}
				if($_POST['delete_s'] > 0)
				{ self::deleteSurvey($_POST['delete_s']); }
				
				return;
			break;
			case 'editor_s':
				if($this->Survey)
				{
					$this->Surveys[$this->Survey->getId()]->setTitle($_POST['title']);
					$this->Survey->setTitle(stripslashes($_POST['title']));
					$this->Survey->setDescription(stripslashes($_POST['description']));
					$this->Survey->setGuide(stripslashes($_POST['guide']));
					$this->Survey->setDataChanged(true);
					$this->Survey->update();
					
					if($this->Survey->Sections)
					{
						if($_POST['activate'] > 0)
						{ 
							$this->Survey->Sections[$_POST['activate']]->setActive(1);
							$commander->setTableActive($_POST['activate'], 'survey_sections', 1); 
						}
						if($_POST['deactivate'] > 0)
						{ 
							$this->Survey->Sections[$_POST['deactivate']]->setActive(0);
							$commander->setTableActive($_POST['deactivate'], 'survey_sections', 0);  
						}
					}
					
					if($_POST['sec_id'])
					{
						$i=0;
						foreach($_POST['sec_id'] as $id)
						{
							if(stripslashes($_POST['sec_title'][$i]) != $this->Survey->Sections[$id]->getTitle())
							{ 
								$this->Survey->Sections[$id]->setTitle(stripslashes($_POST['sec_title'][$i])); 
								$this->Survey->Sections[$id]->setDataChanged(true);
							}
							if($_POST['sec_type'][$i] != $this->Survey->Sections[$id]->getType())
							{ 
								$this->Survey->Sections[$id]->setType($_POST['sec_type'][$i]); 
								$this->Survey->Sections[$id]->setDataChanged(true);
							}
							if($_POST['sec_order'][$i] != $this->Survey->Sections[$id]->Order)
							{ 
								$commander->setTableItemOrder($id, $this->Survey->Sections[$id]->getOrder(), $_POST['sec_order'][$i], 'survey_sections', 'section_order', 'survey_id'); 
								$this->Survey->setSubLoaded(false);
							}
							$this->Survey->Sections[$id]->update();
							$i++;
						}
						if($_POST['delete_sec'] > 0)
						{ 
							self::deleteSurveySection($_POST['delete_sec']);
							$this->Survey->setSubLoaded(false);
						}
						if($_POST['up'] > 0)
						{ 
							$commander->setTableItemOrderUp($_POST['up'], 'survey_sections', 'section_order', 'survey_id'); 
							$this->Survey->setSubLoaded(false);
						}
						if($_POST['down'] > 0)
						{ 
							$commander->setTableItemOrderDown($_POST['down'], 'survey_sections', 'section_order', 'survey_id'); 
							$this->Survey->setSubLoaded(false);
						}
						if($_POST['selected'])
						{
							foreach($_POST['selected'] as $id)
							{
								if(isset($this->Survey->Sections[$id]))
								{
									if($_POST['activate'] == '-1')
									{
										$this->Survey->Sections[$id]->setActive(1);
										$commander->setTableActive($id, 'survey_sections', 1); 
									}
									if($_POST['deactivate'] == '-1')
									{
										$this->Survey->Sections[$id]->setActive(0);
										$commander->setTableActive($id, 'survey_sections', 0); 
									}
								}
							}
						}
					}
					
					if($_POST['dem_id'])
					{
						$i=0;
						foreach($_POST['dem_id'] as $id)
						{
							if(stripslashes($_POST['dem_title'][$i]) != $this->Survey->Demogs[$id]->getTitle())
							{ 
								$this->Survey->Demogs[$id]->setTitle(stripslashes($_POST['dem_title'][$i])); 
								$this->Survey->Demogs[$id]->setDataChanged(true);
							}
							
							if($_POST['new_dem_'.$id.'_val'] != '')
							{ 
								self::addSurveyDemogValue($this->Survey->getId(), $id, $_POST['new_dem_'.$id.'_val']);
								$this->Survey->setSubLoaded(false);
							}
				
							$this->Survey->Demogs[$id]->update();
							$i++;
						}
						if($_POST['delete_dem'] > 0)
						{ 
							self::deleteSurveyDemog($_POST['delete_dem']);
							$this->Survey->setSubLoaded(false);
						}
					}
					
					if($_POST['dem_val_id'])
					{
						$i=0;
						foreach($_POST['dem_val_id'] as $id)
						{
							if($_POST['dem_val_value'][$i] != $this->Survey->DemogValues[$id]->Value)
							{ 
								$this->Survey->DemogValues[$id]->setValue($_POST['dem_val_value'][$i]); 
								$this->Survey->DemogValues[$id]->setDataChanged(true);
								$this->Survey->DemogValues[$id]->update();
							}
							$i++;
						}
						if($_POST['delete_dem_val'] > 0)
						{ 
							self::deleteSurveyDemogVal($_POST['delete_dem_val']);
							$this->Survey->setSubLoaded(false);
						}
					}
					
					if($this->Survey->getSubLoaded() == false)
					{ 
						$this->Survey->loadSections();
						$this->Survey->loadDemogs();
					}
				}
				return;
			break;
			case 'editor_sec':
				if(isset($this->Survey->Section))
				{
					if(stripslashes($_POST['title']) != $this->Survey->Section->getTitle())
					{ 
						$this->Survey->Section->setTitle(stripslashes($_POST['title'])); 
						$this->Survey->Section->setDataChanged(true);
					}
					if(stripslashes($_POST['guide']) != $this->Survey->Section->getGuide())
					{ 
						$this->Survey->Section->setGuide(stripslashes($_POST['guide'])); 
						$this->Survey->Section->setDataChanged(true);
					}
					$this->Survey->Section->update();
					
					if(isset($this->Survey->Section->Questions))
					{
						if($_POST['ques_id'])
						{
							$i=0;
							foreach($_POST['ques_id'] as $id)
							{
								if(isset($this->Survey->Section->Questions[$id]))
								{
									if(stripslashes($_POST['ques_question'][$i]) != $this->Survey->Section->Questions[$id]->getQuestion())
									{ 
										$this->Survey->Section->Questions[$id]->setQuestion(stripslashes($_POST['ques_question'][$i])); 
										$this->Survey->Section->Questions[$id]->setDataChanged(true);
									}
									if(stripslashes($_POST['ques_question_big'][$i]) != $this->Survey->Section->Questions[$id]->getQuestionLong())
									{ 
										$this->Survey->Section->Questions[$id]->setQuestionLong(stripslashes($_POST['ques_question_big'][$i])); 
										$this->Survey->Section->Questions[$id]->setDataChanged(true);
									}
									
									if($_POST['ques_type'][$i] != $this->Survey->Section->Questions[$id]->getType())
									{ 
										$this->Survey->Section->Questions[$id]->setType($_POST['ques_type'][$i]); 
										$this->Survey->Section->Questions[$id]->setDataChanged(true);
									}
									if(is_numeric($_POST['ques_min_val'][$i]) && ($_POST['ques_min_val'][$i] != $this->Survey->Section->Questions[$id]->getMinValue()))
									{ 
										$this->Survey->Section->Questions[$id]->setMinValue($_POST['ques_min_val'][$i]); 
										$this->Survey->Section->Questions[$id]->setDataChanged(true);
									}
									if(is_numeric($_POST['ques_max_val'][$i]) && ($_POST['ques_max_val'][$i] != $this->Survey->Section->Questions[$id]->getMaxValue()))
									{ 
										$this->Survey->Section->Questions[$id]->setMaxValue($_POST['ques_max_val'][$i]); 
										$this->Survey->Section->Questions[$id]->setDataChanged(true);
									}
									if($_POST['ques_order'][$i] != $this->Survey->Section->Questions[$id]->Order)
									{ 
										$commander->setTableItemOrder($id, $this->Survey->Section->Questions[$id]->Order, $_POST['ques_order'][$i], 'survey_question', 'question_order', 'section_id'); 
										$this->Survey->Section->setSubLoaded(false);
									}
									$this->Survey->Section->Questions[$id]->update();
								}
								$i++;
							}
							
							if($_POST['activate'] > 0)
							{ 
								$this->Survey->Section->Questions[$_POST['activate']]->setActive(1);
								$commander->setTableActive($_POST['activate'], 'survey_question', 1); 
							}
							if($_POST['deactivate'] > 0)
							{ 
								$this->Survey->Section->Questions[$_POST['deactivate']]->setActive(0);
								$commander->setTableActive($_POST['deactivate'], 'survey_question', 0);  
							}
							
							if($_POST['delete_ques'] > 0)
							{ 
								self::deleteSurveyQuestion($_POST['delete_ques']);  
								$this->Survey->Section->setSubLoaded(false);
							}
							
							if($_POST['up'] > 0)
							{ 
								$commander->setTableItemOrderUp($_POST['up'], 'survey_question', 'question_order', 'section_id'); 
								$this->Survey->Section->setSubLoaded(false);
							}
							if($_POST['down'] > 0)
							{ 
								$commander->setTableItemOrderDown($_POST['down'], 'survey_question', 'question_order', 'section_id'); 
								$this->Survey->Section->setSubLoaded(false);
							}
							
							if($_POST['selected'])
							{
								foreach($_POST['selected'] as $id)
								{
									if(isset($this->Survey->Section->Questions[$id]))
									{
										if($_POST['activate'] == '-1')
										{
											$this->Survey->Section->Questions[$id]->setActive(1);
											$commander->setTableActive($id, 'survey_question', 1); 
										}
										if($_POST['deactivate'] == '-1')
										{
											$this->Survey->Section->Questions[$id]->setActive(0);
											$commander->setTableActive($id, 'survey_question', 0); 
										}
									}
								}
							}
						}
						if($this->Survey->Section->getSubLoaded() == false)
						{ $this->Survey->Section->loadQuestions(); }
					}
				}
				return;
			break;
			
			
			case 'answers_s':
				if($_POST['f_js_valid'] == 1)
				{
					if($this->Survey->Sections)
					{
						$this->Response = new SurveyResponse();
						$this->Response->setSurveyId($this->Survey->Id);
						$this->Response->setSubject($_POST['response_subject']);
						if($_GET['r'] > 0)
						{ $this->Response->setId($_GET['r']); }
						
						if($this->Survey->Demogs)
						{
							foreach($this->Survey->Demogs as $demog)
							{ 
								if($demog->Values)
								{
									$this->Response->DemographyValues[$demog->Id] = new ResponseDemographyValue();
									$this->Response->DemographyValues[$demog->Id]->setSurveyId($this->Survey->Id);
									$this->Response->DemographyValues[$demog->Id]->setValue($_POST['demog_'.$demog->Id]);
								}
							}
						}
					
						$questions=0;
						$i=0;
						foreach($this->Survey->Sections as $section)
						{
							if($section->Questions)
							{
								$questions += count($section->Questions);
								foreach($section->Questions as $question)
                				{
									switch($section->Type)
									{
										case 0:
											$this->Response->AnswerValuesScale[$i] = new AnswerValueScale();
											$this->Response->AnswerValuesScale[$i]->setQuestionId($question->Id);
											if($question->isValidAnswer($_POST['q_'.$question->Id.'_01']) && $question->isValidAnswer($_POST['q_'.$question->Id.'_02']))
											{ $this->Response->AnswerValuesScale[$i]->setValid(true); }
											if($this->Response->AnswerValuesScale[$i]->getValid())
											{
												$this->Response->AnswerValuesScale[$i]->setValue($_POST['q_'.$question->Id.'_01']);
												$this->Response->AnswerValuesScale[$i]->setValue2($_POST['q_'.$question->Id.'_02']);
											}
											$i++;
										break;
										case 1:
											$this->Response->AnswerValuesScale[$i] = new AnswerValueScale();
											$this->Response->AnswerValuesScale[$i]->setQuestionId($question->Id);
											$this->Response->AnswerValuesScale[$i]->setValid($question->isValidAnswer($_POST['q_'.$question->Id.'_1']));
											if($this->Response->AnswerValuesScale[$i]->getValid())
											{ $this->Response->AnswerValuesScale[$i]->setValue($_POST['q_'.$question->Id.'_1']); }
											$i++;
										break;
										case 2:
											$this->Response->AnswerValuesScale[$i] = new AnswerValueScale();
											$this->Response->AnswerValuesScale[$i]->setQuestionId($question->Id);
											$this->Response->AnswerValuesScale[$i]->setValid($question->isValidAnswer($_POST['q_'.$question->Id.'_2']));
											if($this->Response->AnswerValuesScale[$i]->getValid())
											{ $this->Response->AnswerValuesScale[$i]->setValue($_POST['q_'.$question->Id.'_2']); }
											$i++;
										break;
									}
								}
							}
						}
						$this->Response->setQuesCount($questions);
						
						if($this->Response->checkSetValid())
						{ $this->Response->insertResponse(); }
						if($_GET['r'] > 0)
						{ $this->Survey->loadResponse($_GET['r']); }
					}
					
				}
				
				return;
			break;
		}
		if($this->SubLoaded == false)
		{ self::loadSurveys(); }
	}
	
	public function addSurvey($Title = 'New Survey')
	{
		global $commander;
		$commander->stripNonAlphaNum($Title);
		if($Title)
		{
			$sql = 'INSERT INTO '.DB_PERFIX.'survey (`site_id` ,`title`) VALUES ('.$this->SiteId.', "'.$Title.'");';
			$result = mysql_query($sql);
			$this->SubLoaded = false;
		}
	}
	
	public function deleteSurvey($id)
	{
		if($id > 0)
		{
			$sql = 'DELETE FROM '.DB_PERFIX.'survey_question WHERE survey_id='.$id.';';
			$result = mysql_query($sql);
			
			$sql = 'DELETE FROM '.DB_PERFIX.'survey_sections WHERE survey_id='.$id.';';
			$result = mysql_query($sql);
			
			$sql = 'DELETE FROM '.DB_PERFIX.'survey WHERE site_id='.$this->SiteId.' AND id='.$id.';';
			$result = mysql_query($sql);
			unset($this->Surveys[$id]);
		}
	}
	
	public function addSurveySection($surveyId, $type=0, $Title = 'New Section')
	{
		global $commander;
		$commander->stripNonAlphaNum($Title);
		if($Title)
		{
			$next = $commander->makeTableItemsOrder($surveyId, 'survey_sections', 'section_order', 'survey_id');;
			
			$sql = 'INSERT INTO '.DB_PERFIX.'survey_sections (`survey_id` ,`section_type`, `title`, `section_order`) VALUES ('.$surveyId.', '.$type.', "'.$Title.'", '.$next.');';
			$result = mysql_query($sql);
			$this->SubLoaded = false;
		}
	}
	
	public function addSurveyDemog($surveyId, $Title = 'New Demographics')
	{
		global $commander;
		$commander->stripNonAlphaNum($Title);
		if($Title)
		{
			$sql = 'INSERT INTO '.DB_PERFIX.'survey_demography (`survey_id`, `title`) VALUES ('.$surveyId.', "'.$Title.'");';
			$result = mysql_query($sql);
			$this->SubLoaded = false;
		}
	}
	
	public function addSurveyDemogValue($surveyId, $demId, $value = 'New value')
	{
		global $commander;
		$commander->stripNonAlphaNum($value);
		if(($value != '') && ($surveyId > 0) && ($demId > 0))
		{
			$sql = 'INSERT INTO '.DB_PERFIX.'survey_demography_values (`survey_id`, `demography_id`, `value`) VALUES ('.$surveyId.', '.$demId.', "'.$value.'");';
			$result = mysql_query($sql);
			$this->SubLoaded = false;
		}
	}
	
	public function deleteSurveySection($id)
	{
		global $commander;
		if(($id > 0) && ($this->Survey->Sections[$id]->getId() == $id))
		{
			$sql = 'DELETE FROM '.DB_PERFIX.'survey_question WHERE section_id='.$id.';';
			$result = mysql_query($sql);
			
			$sql = 'DELETE FROM '.DB_PERFIX.'survey_sections WHERE id='.$id.' LIMIT 1;';
			$result = mysql_query($sql);
			unset($this->Survey->Sections[$id]);
			
			$next = $commander->makeTableItemsOrder($this->Survey->Id, 'survey_sections', 'section_order', 'survey_id');
		}
	}
	
	public function deleteSurveyDemog($id)
	{
		if($id > 0)
		{
			$sql = 'DELETE FROM '.DB_PERFIX.'survey_demography_values WHERE demography_id='.$id.';';
			$result = mysql_query($sql);
			
			$sql = 'DELETE FROM '.DB_PERFIX.'survey_demography WHERE id='.$id.' LIMIT 1;';
			$result = mysql_query($sql);
			unset($this->Survey->Demogs[$id]);
		}
	}
	
	public function deleteSurveyDemogVal($id)
	{
		if($id > 0)
		{
			$sql = 'DELETE FROM '.DB_PERFIX.'survey_demography_values WHERE id='.$id.';';
			$result = mysql_query($sql);
		}
	}
	
	public function addSurveyQuestion($surveyId, $sectionId=0, $type=0, $question = 'New Question')
	{
		global $commander;
		$commander->stripNonAlphaNum($question);
		if(($question != '') && ($surveyId > 0) && ($sectionId > 0))
		{
			$next = $commander->makeTableItemsOrder($sectionId, 'survey_question', 'question_order', 'section_id');
			
			$sql  = 'INSERT INTO '.DB_PERFIX.'survey_question ';
			$sql .= '(`survey_id`,`section_id`,`question_type`, `question`, `question_order`) VALUES ';
			$sql .= '('.$surveyId.', '.$sectionId.', '.$type.', "'.$question.'", '.$next.');';
			$result = mysql_query($sql);
			$this->SubLoaded = false;
		}
	}
	
	public function deleteSurveyQuestion($id)
	{
		global $commander;
		if(isset($this->Survey->Section->Questions[$id]))
		{
			$sql = 'DELETE FROM '.DB_PERFIX.'survey_question WHERE id='.$id.' LIMIT 1;';
			$result = mysql_query($sql);
			unset($this->Survey->Section->Questions[$id]);
			
			$commander->makeTableItemsOrder($this->Survey->Section->getId(), 'survey_question', 'question_order', 'section_id');
		}
	}
}

?>